package com.tiger.novelcrazyper;

import com.tiger.novelcrazyper.base.BaseActivity;
import com.tiger.novelcrazyper.base.CommonUtil;
import com.tiger.novelcrazyper.base.DataServer;
import com.tiger.novelcrazyper.base.DataServerFactory;
import com.tiger.novelcrazyper.base.DataServer.IServerResponseListener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ReadActivity extends BaseActivity {
	private DataServer server = DataServer.getInstance(this);
	private DataServerFactory serverData = DataServerFactory.getInstance();
	private Button pre,next;
	private TextView title,content;
	private ScrollView contentScroll;
	private LinearLayout pageTool;
	private ProgressDialog pd_downloading;
	private int num=1;
	private ServerResponseListener listener=new ServerResponseListener();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.read);
        server.addListener(CommonUtil.listnerKeys.read_key, listener);
        CommonUtil.setAdView(this, this);
        initView();
    }
    
    @Override
	protected void onDestroy() {
    	server.removeListener(CommonUtil.listnerKeys.read_key);
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	@Override
	protected void onPause() {
		server.removeListener(CommonUtil.listnerKeys.read_key);
		// TODO Auto-generated method stub
		super.onPause();
	}
	@Override
	protected void onResume() {
		server.addListener(CommonUtil.listnerKeys.read_key, listener);
		// TODO Auto-generated method stub
		super.onResume();
	}
	@Override
	protected void onStop() {
		server.removeListener(CommonUtil.listnerKeys.read_key);
		// TODO Auto-generated method stub
		super.onStop();
	}
	boolean isInThread=false;
	class ToolThread implements Runnable{

		@Override
		public void run() {
			isInThread=true;
			pageTool.setVisibility(View.VISIBLE);
			Log.i("cra", "visiable");
			// TODO Auto-generated method stub
			setToolVisiable();
		}
    	
    };
    private void setToolVisiable()
    {
    	try{
    		Thread.sleep(3000);
    		pageTool.setVisibility(View.GONE);
    		Log.i("cra", "GONE");
    		isInThread=false;
    	}catch(Exception exp){
    		exp.printStackTrace();
    	}
    }
    public void initView(){
    	pageTool=(LinearLayout)this.findViewById(R.id.pageTool);
    	contentScroll=(ScrollView)this.findViewById(R.id.contentScroll);
    	contentScroll.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if(!isInThread){
					Log.i("cra", "isInThread........");
//				new Thread().start();
				pageTool.post(new ToolThread());
				}
				return false;
			}
    		
    	});
    	pre=(Button)this.findViewById(R.id.pre);
    	next=(Button)this.findViewById(R.id.suf);
    	content=(TextView)this.findViewById(R.id.book_content);
    	title=(TextView)this.findViewById(R.id.title);
    	pre.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(num==1){
					Toast.makeText(ReadActivity.this, "已是第一章", Toast.LENGTH_LONG).show();
					return;
				}
				num--;
				readTxtByNum(""+num);
			}
    	});
    	next.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(num==9){
					Toast.makeText(ReadActivity.this, "已是最后一章", Toast.LENGTH_LONG).show();
					return;
				}
				num++;
				readTxtByNum(""+num);
			}
    	});
    	readTxtByNum(""+num);
    }
    private void readTxtByNum(String num){
    	pd_downloading = ProgressDialog.show(ReadActivity.this,
				null, "正在读取小说资源", true, true);
    	server.sendReqGetTxt(CommonUtil.sendMessage.READ_TXT, num);
    }
    String con="";
    Handler handler=new Handler(){
    	@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CommonUtil.sendMessage.READ_TXT:
				con=CommonUtil.getTextByTxt(ReadActivity.this, ""+num);
				content.setText(con);
				title.setText(ReadActivity.this.getString(R.string.app_name)+"(第"+num+"章)");
				if(pd_downloading!=null)
				{
					pd_downloading.dismiss();
					pd_downloading=null;
				}
				break;
			}
    	}
    };
    public class ServerResponseListener implements IServerResponseListener{

		@Override
		public void handleResponse(int reqId, boolean result) {
			// TODO Auto-generated method stub
			if (result) {
				switch (reqId) {
				case CommonUtil.sendMessage.READ_TXT:
					con=serverData.getTxtContent();
					handler.sendEmptyMessage(CommonUtil.sendMessage.READ_TXT);
					break;
				default:
					break;
				}
			} else {
				if(pd_downloading!=null)
				{
					pd_downloading.dismiss();
					pd_downloading=null;
				}
				Toast.makeText(ReadActivity.this, "读取小说资源失败，请重试",
						Toast.LENGTH_SHORT).show();
			}
		}
		
	}
}